package article

import (
	"go-gin-api/app/model"
	"go-gin-api/app/util"

	"github.com/gin-gonic/gin"
)

// GetDetail 获取文章
func GetDetail(c *gin.Context) *util.Response {
	var err error
	result := model.Article{}
	err = model.DB.Where("id=?", c.Query("id")).Find(&result).Error
	if err != nil {
		return &util.Response{
			Code:    200,
			Message: "failed",
			Data:    "查询详情失败!",
		}
	}
	types := model.ArticleType{}
	err = model.DB.Where("id=?", result.TypeID).Find(&types).Error
	if err != nil {
		return &util.Response{
			Code:    200,
			Message: "failed",
			Data:    "查询分类失败!",
		}
	}
	result.TypeName = types.TypeName
	user := model.User{}
	err = model.DB.Where("id=?", result.UserID).Find(&user).Error
	if err != nil {
		return &util.Response{
			Code:    200,
			Message: "failed",
			Data:    "查询作者失败!",
		}
	}
	result.UserName = user.NickName
	return &util.Response{
		Code:    200,
		Message: "success",
		Data:    result,
	}
}
